/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: worker_descriptor.cpp
 */

#include <iostream>
#include <string>
#include <exception>

#include "worker_descriptor.hpp"

using namespace middleware;

//-----------------------------------------------------------------------------
// Constructors and destructor
/* Default constructor */
WorkerDescriptor::WorkerDescriptor() {
	this->_address.set_ip("127.0.0.1");
	this->_address.set_port(0);
	this->_capabilities = kDefWorkerCapab;
	this->_status = 0;
}
/* Constructor 1 */
WorkerDescriptor::WorkerDescriptor(Address addr) {
	this->_address = addr;
	this->_capabilities = kDefWorkerCapab;
	this->_status = 0;
}
/* Constructor 2 */
WorkerDescriptor::WorkerDescriptor(Address addr, WorkerCapabilities capab) {
	this->_address = addr;
	this->_capabilities = capab;
	this->_status = 0;
}
/* Copy constructor */
WorkerDescriptor::WorkerDescriptor(const WorkerDescriptor& wdesc) {
	this->_address = wdesc._address;
	this->_capabilities = wdesc._capabilities;
	this->_status = wdesc._status;
}
/* Destructor */
WorkerDescriptor::~WorkerDescriptor() {

}
/* Calculating PI */
calculus::PIType WorkerDescriptor::PI() const {
	calculus::PerformanceEvalParams pep;
	pep.eval_bandwidth = this->_capabilities.capab_bandwidth;
	pep.eval_core_num = this->_capabilities.capab_core_num;
	pep.eval_cpu_mhz = this->_capabilities.capab_cpu_mhz;
	pep.eval_cpu_num = this->_capabilities.capab_cpu_num;
	pep.eval_hops = this->_capabilities.capab_hops;
	pep.eval_load = (unsigned int)this->_status;
	pep.eval_ram_mb = this->_capabilities.capab_ram_mb;
	return calculus::CalculatePI(pep);
}
/* Operators */
bool WorkerDescriptor::operator==(const WorkerDescriptor& wdesc) const {
	bool res;
	res = (this->_capabilities.capab_bandwidth ==
			wdesc._capabilities.capab_bandwidth);
	res = res && (this->_capabilities.capab_core_num ==
			wdesc._capabilities.capab_core_num);
	res = res && (this->_capabilities.capab_cpu_mhz ==
			wdesc._capabilities.capab_cpu_mhz);
	res = res && (this->_capabilities.capab_cpu_num ==
			wdesc._capabilities.capab_cpu_num);
	res = res && (this->_capabilities.capab_hops ==
			wdesc._capabilities.capab_hops);
	res = res && (this->_capabilities.capab_ram_mb ==
			wdesc._capabilities.capab_ram_mb);
	res = res && (this->_status == wdesc._status);
	return res;
}
bool WorkerDescriptor::operator!=(const WorkerDescriptor& wdesc) const {
	return !((*this) == wdesc);
}
